
/**
 * 动态Form生成功能
 */

/**
 * 生成一个field的html代码
 */
function createFieldHtml(prop, datas, className) {
	return formFieldCreator[prop.type.name](prop, datas, className);
}

function createFormFieldHtml(prop, datas, className) {
	$type = prop.type;
	if(prop.type=='line' || prop.type=='string'){
		$type = 'text';
	}
	if(prop.type=='dropdown' || prop.type=='select'){
		$type = 'dropdown';
	}
	if(prop.type in formFieldCreatorMap){
		prop.writable = true || !prop.readOnly
		return formFieldCreatorMap[$type](prop,datas,className);
	}	
}

/**
 * form对应的string/date/long/enum/boolean类型表单组件生成器
 * fp_的意思是form paremeter
 */
var formFieldCreator = {
	'string': function(prop, datas, className) {
		var result = "<td width='120'>" + prop.name + "："+prop.title_subfix+"</td>";
		if (prop.writable === true) {
			result += "<td><input type='text' id='" + prop.id + "' name='" + prop.id + "' class='" + className + "' value='" + prop.value + "' />";
		} else {
			result += "<td>" + prop.value;
		}
		return result;
	},
	'integer': function(prop, datas, className) {
		var result = "<td width='120'>" + prop.name + "："+prop.title_subfix+"</td>";
		if (prop.writable === true) {
			result += "<td><input type='number' id='" + prop.id + "' name='" + prop.id + "' class='" + className + "' value='" + prop.value + "' />";
		} else {
			result += "<td>" + prop.value;
		}
		return result;
	},
	'date': function(prop, datas, className) {
		var result = "<td width='120'>" + prop.name + "："+prop.title_subfix+"</td>";
		if (prop.writable === true) {
			result += "<td><input type='text' id='" + prop.id + "' name='" + prop.id + "' class='date " + className + "' value='" + prop.value + "'/>";
		} else {
			result += "<td>" + prop.value;
		}
		return result;
	},
	'enum': function(prop, datas, className) {
		var result = "<td width='120'>" + prop.name + "："+prop.title_subfix+"</td>";
		if (prop.writable === true) {
			result += "<td><select id='" + prop.id + "' name='" + prop.id + "' class='" + className + "'>";
			$.each(datas[prop.id], function(k, v) {
				result += "<option value='" + k + "'>" + v + "</option>";
			});
			result += "</select>";
		} else {
			result += "<td>" + prop.value;
		}
		return result;
	},
	'users': function(prop, datas, className) {
		var result = "<td width='120'>" + prop.name + "："+prop.title_subfix+"</td>";
		if (prop.writable === true) {
			result += "<td><input type='text' id='" + prop.id + "' name='" + prop.id + "' class='" + className + "' value='" + prop.value + "' />";
		} else {
			result += "<td>" + prop.value;
		}
		return result;
	}
};


var formFieldCreatorMap = {
	'expression': function(prop, datas, className) {
		var result = "<td width='120'>" + prop.name + "："+prop.title_subfix+"</td>";
		if (prop.writable === true) {
			result += "<td><input type='text' id='" + prop.id + "' name='" + prop.id + "' class='" + className + "' value='" + prop.value + "' />";
		} else {
			result += "<td>" + prop.value;
		}
		return result;
	},
	'text': function(prop, datas, className) {
		var result = "<td width='120'>" + prop.name + "："+prop.title_subfix+"</td>";;
		if (prop.writable === true) {
			result += "<td><input type='text' id='" + prop.id + "' name='" + prop.id + "' class='" + className + "' value='" + prop.value + "' />";
		} else {
			result += "<td>" + prop.value;
		}
		return result;
	},
	'integer': function(prop, datas, className) {
		var result = "<td width='120'>" + prop.name + "："+prop.title_subfix+"</td>";
		if (prop.writable === true) {
			result += "<td><input type='number' id='" + prop.id + "' name='" + prop.id + "' class='" + className + "' value='" + prop.value + "' />";
		} else {
			result += "<td>" + prop.value;
		}
		return result;
	},
	'date': function(prop, datas, className) {
		var result = "<td width='120'>" + prop.name + "："+prop.title_subfix+"</td>";
		if (prop.writable === true) {
			result += "<td><input type='text' id='" + prop.id + "' name='" + prop.id + "' class='date " + className + "' value='" + prop.value + "'/>";
		} else {
			result += "<td>" + prop.value;
		}
		return result;
	},
	
	'multi-line-text': function(prop, datas, className) {
		var result = "<td width='120'>" + prop.name + "："+prop.title_subfix+"</td>";
		if (prop.writable === true) {
			result += "<td><textarea  id='" + prop.id + "' name='" + prop.id + "' class='" + className + "' value='" + prop.value + "' />";
		} else {
			result += "<td>" + prop.value;
		}
		return result;
	},
	
	'dropdown': function(prop, datas, className) {
		var result = "<td width='120'>" + prop.name + "："+prop.title_subfix+"</td>";
		if (prop.writable === true) {
			result += "<td><select id='" + prop.id + "' name='" + prop.id + "' class='" + className + "'>";
			$.each(prop.options, function(k, v) {
				result += "<option value='" + v.name + "'>" + v.name + "</option>";
			});
			result += "</select>";
		} else {
			result += "<td>" + prop.value;
		}
		return result;
	},
	'users': function(prop, datas, className) {
		var result = "<td width='120'>" + prop.name + "："+prop.title_subfix+"</td>";
		if (prop.writable === true) {
			result += "<td><input type='text' id='" + prop.id + "' name='" + prop.id + "' class='" + className + "' value='" + prop.value + "' />";
		} else {
			result += "<td>" + prop.value;
		}
		return result;
	}
};
